package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.ChargeDao;
import com.wdy.po.Charge;
import com.wdy.service.ChargeManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings("unchecked")
public class ChargeManagerImpl implements ChargeManager {

	private ChargeDao chargeDao;

	public void setChargeDao(ChargeDao chargeDao) {
		this.chargeDao = chargeDao;
	}

	public void saveCharge(Charge charge) {
		chargeDao.save(charge);
	}

	public void removeCharge(Charge charge) {
		chargeDao.delete(charge);
	}

	@Override
	public void removeCharges(Object[] objects) {
		// TODO Auto-generated method stub

	}

	public void updateCharge(Charge charge) {
		chargeDao.attachDirty(charge);
	}

	public Charge getCharge(Integer id) {
		return chargeDao.findById(id);
	}

	public List<Charge> listAll() {
		return chargeDao.findAll();
	}

	public List<Charge> findByExample(Charge charge) {
		return chargeDao.findByExample(charge);
	}

	public List<Charge> findByDate(String value) {
		return chargeDao.findByDate(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = chargeDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) chargeDao.query(conList, page);
		List<Charge> charges = new LinkedList<Charge>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oList = (Object[]) o;
				for (Object oo : oList) {
					if (oo instanceof Charge) {
						charges.add((Charge) oo);
					}
				}
			} else {
				if (o instanceof Charge) {
					charges.add((Charge) o);
				}
			}
		}
		return new Result(page, charges);
	}

	@Override
	public List query(List<Condition> conList) {
		// TODO Auto-generated method stub
		List tempList = (List) chargeDao.query(conList);
		List<Charge> charges = new LinkedList<Charge>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oList = (Object[]) o;
				for (Object oo : oList) {
					if (oo instanceof Charge) {
						charges.add((Charge) oo);
					}
				}
			} else {
				if (o instanceof Charge) {
					charges.add((Charge) o);
				}
			}
		}
		return charges;
	}
}
